/*
 * TCSS 422 Web Spider Project
 * Group Money: Al McKenzie, Michael Pitts, Taylor Zielske
 */
package model;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * This class can parse single lines of text to tease out any html url data.
 * 
 * @author Michael Pitts
 * @version Oct 24, 2011
 */
public class URLSearchParser {
	
	/**
	* The leading string for url references in a html tag.
	**/
	private static final String HREF = "href=\"";
	
	/**
	 * List of found urls.
	 */
	private final List<URL> my_urls;
	
	/**
	 * Reference url used when decoding relative urls.
	 */
	private final URL my_reference_url;
	
	/**
	 * Creates a new URLSearchParser which will use the given url as the base to form
	 * relative urls into absolute.
	 * @param the_ref_url is the base url to use when decoding relative urls.
	 */
	public URLSearchParser(final URL the_ref_url) {
		my_reference_url = the_ref_url;
		my_urls = new ArrayList<URL>();
	}
	
	/**
	* Given one line of input, checks if there are any html tags to read, and if so
	* stores them for later reference.
	* @param the_line is the line to search for any html tags.
	**/
	public void parseLine(final String the_line) {
		String[] strings = the_line.split(HREF); // potential split, if only one no html
		String url_string;
		URL url;
		String[] splits;
		for (int i = 1; i < strings.length; i++) { // if more than one, at least one tag
			splits = strings[i].split("\"");
			if (splits.length > 0) {
				url_string = splits[0];//finds the end of the url
				try {
					url = new URL(my_reference_url, url_string); // gets absolute from relative
					if (url.getProtocol().equals("http") && isReadable(url.getPath())) {
						url = new URL(url.getProtocol(), url.getHost(), url.getPath());
						my_urls.add(url);
					}
				} catch (MalformedURLException e) {
					// If url is mal-formed, just ignore it
					//e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Returns true if the given path is okay to read.
	 * @param the_path is the path of a given url.
	 * @return true if the given path is okay to read, false otherwise.
	 */
	private boolean isReadable(final String the_path) {
		return the_path.endsWith("/") || the_path.endsWith(".html") || 
		the_path.endsWith(".htm") || the_path.endsWith(".txt");
	}
	
	/**
	 * Gets all of the urls pulled out of the parser.
	 * @return an array of urls that were found while parsing.
	 */
	public URL[] getResults() {
		return my_urls.toArray(new URL[my_urls.size()]);
	}
}
